package com.aiwhose.aiwhosewechatgroup.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class WeMiniAppCustomUtil {

    @Value("${wechat.appid}")
    private String appid;
    @Value("${wechat.secret}")
    private String secret;
    @Value("${wechat.grant_type}")
    private String grant_type;

    @Resource
    RedisUtil redisUtil;

    public String getAccessToken() {

        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("grant_type", "client_credential");
        paramMap.put("appid", appid);
        paramMap.put("secret", secret);
        String accessToken = (String) redisUtil.get("access_token_group_cloud");
        if (ObjectUtil.isNotEmpty(accessToken)) {
            return accessToken;
        } else {
            String result3 = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token", paramMap);
            String newAccessToken = (String) JSONUtil.parseObj(result3).get("access_token");
            redisUtil.set("access_token_group_cloud", newAccessToken, 7200);
            return newAccessToken;
        }
    }


    public Map<String,String> getOpenId(String code) {
        Map<String, String> map = this.OpenId(code);
        String openid = map.get("openid");
        String unionid = map.get("unionid");
        if (StrUtil.isEmpty(openid)) {
            redisUtil.del("access_token_group_cloud");
            return this.OpenId(code);
        }
        return map;
    }

    private Map<String, String> OpenId(String code) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", appid);
        paramMap.put("secret", secret);
        paramMap.put("js_code", code);
        paramMap.put("grant_type", grant_type);
        String result1 = HttpUtil.get("https://api.weixin.qq.com/sns/jscode2session", paramMap);
        JSONObject entries = JSONUtil.parseObj(result1);
        String openid = (String) entries.get("openid");
        String unionid = (String) entries.get("unionid");
        HashMap<String, String> map = new HashMap<>();
        map.put("openid", openid);
        map.put("unionid", unionid);
        return map;
    }

    public String getPhoneNumber(String code) {
        String phoneNumber = this.phoneNumber(code);
        if (StrUtil.isEmpty(phoneNumber)) {
            redisUtil.del("access_token_group_cloud");
            return this.phoneNumber(code);
        }
        return phoneNumber;
    }

    private String phoneNumber(String code) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("code", code);
        String accessToken = this.getAccessToken();
        String result2 = HttpRequest.post("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken)
                .body(JSONUtil.toJsonStr(paramMap))
                .execute().body();
        JSONObject jsonObject = JSONUtil.parseObj(result2);
        if ("ok".equals(jsonObject.get("errmsg"))) {
            Map phoneInfo = jsonObject.getJSONObject("phone_info");
            return (String) phoneInfo.get("phoneNumber");
        }
        return null;
    }

}
